package com.bridgeintelligent.tag.engine.mvc.service.impl;

import com.alibaba.fastjson.JSON;
import com.bridgeintelligent.tag.engine.api.EsDateModel;
import com.bridgeintelligent.tag.engine.mvc.service.IEsDateService;
import com.bridgeintelligent.tag.mapper.EsDateMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class EsDateServiceImpl implements IEsDateService {

    @Autowired
    private EsDateMapper mapper;

    @Override
    public Map<String, String> symbolRelation(String indexId) {
        List<EsDateModel> timeList = mapper.indexAllTime(indexId);
        Map<String,String> map = timeList.stream().collect(Collectors.toMap(EsDateModel::getSymbol,v -> v.getEsSymbol(),(v1,v2)->v1));
        log.info("索引:{} 的ES日期-标识集合：{}", indexId,JSON.toJSONString(map));
        return map;
    }

    @Override
    public Map<String, String> timeSymbolRelation(String indexId) {
        List<EsDateModel> timeList = mapper.indexAllTime(indexId);
        Map<String,String> map = timeList.stream().collect(Collectors.toMap(EsDateModel::getTime,v -> v.getEsSymbol(),(v1,v2)->v1));
        log.info("索引:{} 的ES日期-标识集合：{}", indexId,JSON.toJSONString(map));
        return map;
    }

    @Override
    public String indexAllEsSymbol(String indexId) {
        List<String> list = mapper.indexAllEsSymbol(indexId);
        String symbols = list.stream().collect(Collectors.joining("|"));
        log.info("索引：{} 后缀集：{}",indexId,symbols);
        return symbols;
    }

    @Override
    public String findSymbolByDate(String indexId, String symbol) {
        return mapper.findSymbolByDate(indexId,symbol);
    }

    public static void main(String[] args) {
        List<EsDateModel> list = new ArrayList<>();
        list.add(new EsDateModel().setTime("20231007").setEsSymbol("DAY"));
        list.add(new EsDateModel().setTime("202309").setEsSymbol("MONTH"));
        list.add(new EsDateModel().setTime("202309").setEsSymbol("QUARTER"));
        list.add(new EsDateModel().setTime("202212").setEsSymbol("YEAR"));
        Map<String,String> map = list.stream().collect(Collectors.toMap(EsDateModel::getTime,v -> v.getEsSymbol(),(v1,v2)->v1));
        System.out.println(JSON.toJSONString(map));
    }

}
